package com.lagou.service;

import com.lagou.handle.UserServiceHandle;
import com.lagou.handle.UserServiceHandle2;
import com.lagou.pojo.JSONSerializer;
import com.lagou.pojo.RpcDecoder;
import com.lagou.pojo.RpcEncoder;
import com.lagou.pojo.RpcRequest;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService{
    public String sayHello(String msg) {
        System.out.println("sayHello客户端传递的参数："+ msg);
        return "success: "+ msg;
    }

    @Override
    public String doWork(String id, String name) {
        System.out.println("doWork客户端传递的参数："+ id + "  " + name);
        return "success: " + id + "-" + name;
    }

    public static void startServer(String ip,int port) throws InterruptedException {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workGroup = new NioEventLoopGroup();

        ServerBootstrap serverBootstrap = new ServerBootstrap();

        serverBootstrap.group(bossGroup,workGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                        ChannelPipeline pipeline = nioSocketChannel.pipeline();
                        pipeline.addLast(new StringEncoder());
                        pipeline.addFirst(new StringDecoder());

                        pipeline.addLast(new UserServiceHandle());
                    }
                });

        ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
    }

    //改造为RpcRequest方式
    public static void startServer_rpcRequest(String ip,int port) throws InterruptedException {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workGroup = new NioEventLoopGroup();

        ServerBootstrap serverBootstrap = new ServerBootstrap();

        serverBootstrap.group(bossGroup,workGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                        ChannelPipeline pipeline = nioSocketChannel.pipeline();
                        pipeline.addLast(new StringEncoder());//success
                        pipeline.addLast(new RpcDecoder(RpcRequest.class, new JSONSerializer()));

                        pipeline.addLast(new UserServiceHandle2());
                    }
                });

        ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
    }
}
